﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Caching;
/// <summary>
/// Summary description for ChanelBusiness
/// </summary>
public class ChanelBusiness
{
    ChanelData chanel = new ChanelData();
	public ChanelBusiness()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public bool add(ChannelesEntities item)
    {
        return chanel.add(item);
    }
    public bool edit(ChannelesEntities item)
    {
        return chanel.edit(item);
    }
    public bool delete(int id)
    {
        return chanel.delete(id);
    }
    public bool UpChanel(int id)
    {
        return chanel.UpChanel(id);
    }
    public bool DrownChanel(int id)
    {
        return chanel.DrownChanel(id);
    }
    public ChannelesEntities getItem(int id)
    {
        ChannelesEntities item = null;
        if (id > 0)
        {
            System.Data.SqlClient.SqlDataReader read = chanel.getChanel(id);
            if (read.HasRows)
            {
                if (read.Read())
                {
                    item = new ChannelesEntities();
                    item._id = int.Parse(read["id"].ToString());
                    item._name = read["name"].ToString();
                    item._parent = int.Parse(read["parent"].ToString());
                    item._priority = int.Parse(read["priority"].ToString());
                    item._channelLock = read["lock"].ToString();
                }
                read.Close();
            }
        }
        return item;
    }
    public System.Collections.ArrayList getChanel()
    {
        System.Collections.ArrayList items = new System.Collections.ArrayList();
        System.Data.SqlClient.SqlDataReader read = chanel.getChanel(0);
        
            if (read.HasRows)
            {
                ChannelesEntities item = null;
                while (read.Read())
                {
                    item = new ChannelesEntities();
                    item._id = int.Parse(read["id"].ToString());
                    item._name = read["name"].ToString();
                    item._parent = int.Parse(read["parent"].ToString());
                    item._priority = int.Parse(read["priority"].ToString());
                    item._channelLock = read["lock"].ToString();
                    items.Add(item);
                    item = null;
                }
                read.Close();
            }                         
        return items;
    }

    public System.Collections.ArrayList Menu()
    {
        System.Collections.ArrayList items = new System.Collections.ArrayList();
        System.Collections.ArrayList chanel = this.getChanel();
        if (chanel !=null)
        {
            ChannelesEntities item = null;
            for (int i = 0; i < chanel.Count; i++)
            {
                item = (ChannelesEntities)chanel[i];
                if (!item._channelLock.Trim().Equals("True") && !item._channelLock.Trim().Equals("1"))
                {
                    items.Add(item);
                    item = null;
                }
            }
        }
        return items;
    }

    private System.Collections.ArrayList getchanel_Parent(int parent)
    {
        System.Collections.ArrayList items = new System.Collections.ArrayList();
        System.Collections.ArrayList chanel = this.getChanel();
        if (chanel != null)
        {
            ChannelesEntities item = null;
            for (int i = 0; i < chanel.Count; i++)
            {
                item = (ChannelesEntities)chanel[i];
                if (item._parent == parent)
                {
                    items.Add(item);
                }
                item = null;
            }
        }
        return items;
    }

    public int Max(int parent)
    {
        int result = 0;
        System.Collections.ArrayList chanel = this.getChanel();
        if (chanel != null)
        {
            System.Collections.ArrayList items = this.getchanel_Parent(parent);
            if (items != null && items.Count != 0)
            {
                ChannelesEntities tmp = (ChannelesEntities)items[0];
                result = tmp._priority;
                ChannelesEntities item = null;
                for (int i = 0; i < items.Count; i++)
                {
                    item = (ChannelesEntities)items[i];
                    if (result < item._priority)
                    {
                        result = item._priority;
                    }
                    item = null;
                }
            }
        }
        return result;
    }
    public int Min(int parent)
    {
        int result = 0;
        System.Collections.ArrayList chanel = this.getChanel();
        if (chanel != null)
        {
            System.Collections.ArrayList items = this.getchanel_Parent(parent);
            if (items != null && items.Count != 0)
            {
                ChannelesEntities tmp = (ChannelesEntities)items[0];
                result = tmp._priority;
                ChannelesEntities item = null;
                for (int i = 0; i < items.Count; i++)
                {
                    item = (ChannelesEntities)items[i];
                    if (result > item._priority)
                    {
                        result = item._priority;
                    }
                    item = null;
                }
            }
        }
        return result;
    }
}
